package com.woniuxy.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * 取出实体类的值和属性
 */
public class Object2MapUtil {

    public static Map<String, String> object2Map(Object o) {
        Map<String, String> map = new HashMap<>();

        Field[] fields = o.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);

            try {
                if (field.get(o) != null)
                    map.put(string2Hump(field.getName()), field.get(o).toString());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    static String string2Hump(String source) {


        String ss = "";
        char[] charArray = source.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] >= 'A' && charArray[i] <= 'Z') {
                ss += "_" + charArray[i];
            } else {
                ss += charArray[i];
            }
        }
        return ss.toLowerCase();
    }

//    public static void main(String[] args) {
//        System.out.println(string2Hump("adsDjksgJlisdgfDdd"));
//    }

}
